package com.zenkeeuk.bookerfornfc.client.android.book;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zenkeeuk.bookerfornfc.client.android.Contents;
import com.zenkeeuk.bookerfornfc.client.android.R;

import android.content.Context;
import android.widget.SimpleAdapter;

public class BookCommentsAdapter extends SimpleAdapter {
	
	private static List<Map<String, Object>> comments;
    
    public BookCommentsAdapter(Context context, List<DoubanBookComment> data) {
    	super(context, getData(data), 
    			R.layout.bookcomments, 
    			new String[]{Contents.BOOK_COMMENT_TITLE_KEY, 
    						 Contents.BOOK_COMMENT_RATING_KEY, 
    						 Contents.BOOK_COMMENT_author_KEY,
    						 Contents.BOOK_COMMENT_SUMMARY_KEY }, 
    			new int[]{R.id.comment_title, 
    					  R.id.comment_rating, 
    					  R.id.comment_author,
    					  R.id.comment_summary });
    }

    private static List<Map<String, Object>> getData(List<DoubanBookComment> data) {
    	comments = new ArrayList<Map<String, Object>>();
    	
    	for (int i=0;i<data.size();i++) {
	    	HashMap<String, Object> map = new HashMap<String, Object>();
	    	map.put(Contents.BOOK_COMMENT_TITLE_KEY, data.get(i).getTitle());
	    	
	    	if (data.get(i).getRating() != 0) {
            	map.put(Contents.BOOK_COMMENT_RATING_KEY, data.get(i).getRating());
            } else {
            	map.put(Contents.BOOK_COMMENT_RATING_KEY, Contents.DOUBAN_NO_RATING);
            }
	    	
	    	map.put(Contents.BOOK_COMMENT_author_KEY, data.get(i).getAuthor());
	    	map.put(Contents.BOOK_COMMENT_SUMMARY_KEY, data.get(i).getSummary());
	    	
	    	comments.add(map);
    	}
    	
    	return comments;
    }

}